package v1

import (
	"auth/bll"
	"auth/model"
	"auth/server/web/middleware"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

var UserAuthority = &userAuthority{}

type userAuthority struct{}

func (a *userAuthority) Init(r *gin.RouterGroup) {
	g := r.Group("/userAuthority", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/list", a.list)
	}
}

func (a *userAuthority) create(c *gin.Context) {
	var (
		in  = &model.UserAuthorityCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserAuthority.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *userAuthority) list(c *gin.Context) {
	var (
		in  = &model.UserAuthorityListRequest{}
		out = &model.UserAuthorityListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.UserAuthority.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}
